package com.bwie.parkdemo.mvvm.viewmodel;

import android.app.Application;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.util.Log;
import android.view.View;

import com.bwie.lib_core.action.ActActionField;
import com.bwie.lib_core.base.BaseEntity;
import com.bwie.lib_core.mvvm.base.BaseOBViewModel;
import com.bwie.parkdemo.mvvm.model.AddVisitorModel;
import com.bwie.parkdemo.mvvm.model.entity.LoginEntity;
import com.bwie.parkdemo.mvvm.model.request_entity.AddVisitorBindEntity;
import com.bwie.parkdemo.mvvm.ui.activity.ApplyParkingActivity;
import com.bwie.parkdemo.mvvm.ui.activity.ParkingAllActivity;
import com.google.gson.Gson;

import java.util.HashMap;
import java.util.Map;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.functions.Consumer;
import io.reactivex.schedulers.Schedulers;
import okhttp3.MediaType;
import okhttp3.RequestBody;

/**
 * 添加访客申请的viewmodel
 * */
public class AddVisitorVideModel extends BaseOBViewModel<AddVisitorModel>{

    private AddVisitorBindEntity addVisitorBindEntity;//当前viewmodel全部需要使用的变量封装
    public UIClickListener listener;
    public Bundle bundle;

    public AddVisitorBindEntity getAddVisitorBindEntity() {
        return addVisitorBindEntity;
    }

    public void setAddVisitorBindEntity(AddVisitorBindEntity addVisitorBindEntity) {
        this.addVisitorBindEntity = addVisitorBindEntity;
    }

    public AddVisitorVideModel(@NonNull Application application) {
        super(application);
        addVisitorBindEntity = new AddVisitorBindEntity();
        addVisitorBindEntity.setPartName("请选择车位");
        addVisitorBindEntity.setCarNum("请选择车牌");
        listener = new UIClickListener();

    }

    @Override
    protected AddVisitorModel createModel() {
        return new AddVisitorModel();
    }

    @Override
    protected void initData() {
        //获取对应用户信息->model层的addvisitormodel中获取
        LoginEntity.Values userValues = m.requestUser(getApplication());
        addVisitorBindEntity.userId = userValues.uId;
        addVisitorBindEntity.setUserName(userValues.uName);
        addVisitorBindEntity.setDepartment("商务部");
        if (bundle!=null){
            addVisitorBindEntity.startTime = bundle.getString("startTime");
            addVisitorBindEntity.endTime = bundle.getString("endTime");
            addVisitorBindEntity.setData(""+addVisitorBindEntity.startTime+addVisitorBindEntity.endTime);
            addVisitorBindEntity.setCarNum(bundle.getString("carNum"));
            addVisitorBindEntity.setNowTime(""+bundle.getString("nowTime"));
            addVisitorBindEntity.setTime(""+bundle.getString("time"));
            addVisitorBindEntity.setPartName(""+bundle.getString("partName"));
            addVisitorBindEntity.parkId = bundle.getInt("parkId");
            addVisitorBindEntity.setDescribe(bundle.getString("describe"));
        }
    }

    @Override
    public void onNext(BaseEntity baseEntity) {
        if (baseEntity.statuesCode.equals("200") ){
            showMsg(baseEntity.msg);
            finishAct();
        }
    }

    //当前页面监听器->添加车辆信息监听(复用申请车位中的添加车辆信息界面);
    //添加车位信息监听->复用全部车位列表界面
    //提交按钮->提交到服务器添加来访申请接口
    public class UIClickListener{
        public void startAddCar(){
            if (addVisitorBindEntity.parkId == 0){
                showMsg("请先填写车辆信息");
                return;
            }
            Map<String,Object>map = new HashMap<>();
            map.put(ActActionField.ACTCLAZZ, ApplyParkingActivity.class);
            map.put(ActActionField.FORRESULT,true);
            Bundle bundle = new Bundle();
            bundle.putBoolean(ActActionField.FORRESULT,true);
            bundle.putInt(ActActionField.ACTBUNDLE,addVisitorBindEntity.parkId);
            bundle.putString("parkName",addVisitorBindEntity.getPartName());
            map.put(ActActionField.ACTBUNDLE,bundle);
            startAct(map);
        }
        public void startPart(){
            Map<String,Object>map = new HashMap<>();
            map.put(ActActionField.ACTCLAZZ, ParkingAllActivity.class);
            map.put(ActActionField.FORRESULT,true);
            Bundle bundle = new Bundle();
            bundle.putBoolean(ActActionField.FORRESULT,true);
            map.put(ActActionField.ACTBUNDLE,bundle);
            startAct(map);
        }
        public void startCommit(){
            //访客申请提交到服务器
            /**
             * {
             "carNum": "string",
             "parkId": 0,
             "userId": 0,
             "visitorDescribe": "string",
             "visitorEnd": "string",
             "visitorStart": "string",
             "visitorTime": "string"
             }
             * */
            Map<String,Object>map = new HashMap<>();
            map.put("carNum",addVisitorBindEntity.getCarNum());
            map.put("parkId",addVisitorBindEntity.parkId);
            map.put("userId",addVisitorBindEntity.userId);
            map.put("visitorDescribe",addVisitorBindEntity.getDescribe());
            map.put("visitorEnd",addVisitorBindEntity.endTime);
            map.put("visitorStart",addVisitorBindEntity.startTime);
            map.put("visitorTime",addVisitorBindEntity.getNowTime());
            String jsonStr = new Gson().toJson(map);
            Log.e("ZXWQ","startCommit():"+jsonStr);
            RequestBody body = RequestBody.create(MediaType.parse("application/json"),jsonStr);
            m.request(body).subscribeOn(Schedulers.io())
                    .observeOn(AndroidSchedulers.mainThread())
                    .subscribe(AddVisitorVideModel.this);
        }
    }
}
